package cn.csc.module.product.controller.admin.collect;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.collect.vo.*;
import cn.csc.module.product.convert.collect.SpuCollectConvert;
import cn.csc.module.product.dal.dataobject.collect.SpuCollectDO;
import cn.csc.module.product.service.collect.SpuCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 收藏商品信息")
@RestController
@RequestMapping("/product/spu-collect")
@Validated
public class SpuCollectController {

    @Resource
    private SpuCollectService spuCollectService;

    @PostMapping("/create")
    @ApiOperation("创建收藏商品信息")
    @PreAuthorize("@ss.hasPermission('product:spu-collect:create')")
    public CommonResult<BigInteger> createSpuCollect(@Valid @RequestBody SpuCollectCreateReqVO createReqVO) {
        return success(spuCollectService.createSpuCollect(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新收藏商品信息")
    @PreAuthorize("@ss.hasPermission('product:spu-collect:update')")
    public CommonResult<Boolean> updateSpuCollect(@Valid @RequestBody SpuCollectUpdateReqVO updateReqVO) {
        spuCollectService.updateSpuCollect(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除收藏商品信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-collect:delete')")
    public CommonResult<Boolean> deleteSpuCollect(@RequestParam("id") BigInteger id) {
        spuCollectService.deleteSpuCollect(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得收藏商品信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-collect:query')")
    public CommonResult<SpuCollectRespVO> getSpuCollect(@RequestParam("id") BigInteger id) {
        SpuCollectDO spuCollect = spuCollectService.getSpuCollect(id);
        return success(SpuCollectConvert.INSTANCE.convert(spuCollect));
    }

    @GetMapping("/list")
    @ApiOperation("获得收藏商品信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:spu-collect:query')")
    public CommonResult<List<SpuCollectRespVO>> getSpuCollectList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SpuCollectDO> list = spuCollectService.getSpuCollectList(ids);
        return success(SpuCollectConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得收藏商品信息分页")
    @PreAuthorize("@ss.hasPermission('product:spu-collect:query')")
    public CommonResult<PageResult<SpuCollectRespVO>> getSpuCollectPage(@Valid SpuCollectPageReqVO pageVO) {
        PageResult<SpuCollectDO> pageResult = spuCollectService.getSpuCollectPage(pageVO);
        return success(SpuCollectConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出收藏商品信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:spu-collect:export')")
    @OperateLog(type = EXPORT)
    public void exportSpuCollectExcel(@Valid SpuCollectExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SpuCollectDO> list = spuCollectService.getSpuCollectList(exportReqVO);
        // 导出 Excel
        List<SpuCollectExcelVO> datas = SpuCollectConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "收藏商品信息.xls", "数据", SpuCollectExcelVO.class, datas);
    }

}
